import axios, { AxiosPromise } from 'axios';
import type { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import qs from 'qs';
// 加载nprogress
import NProgress from '../plugins/npropress/index';
import { loadEnv } from '/@/utils/disposeEnv';

import { useUserStore } from '/@/store/modules/userStore';

// 加载环境变量，取得相应的值
/**
 * @param {
 *  VITE_PROXY_DOMAIN - 开发模式下的默认路径
 *  VITE_PROXY_DOMAIN_REAL - 生产模式下的默认路径
 * }
 */
const { VITE_PROXY_DOMAIN, VITE_PROXY_DOMAIN_REAL } = loadEnv()



// 默认配置
const defultConfig: AxiosRequestConfig = {
    // baseURL: process.env.NODE_ENV === "production" ? VITE_PROXY_DOMAIN_REAL : VITE_PROXY_DOMAIN,
    // 使用mock时 baseUrl 置空
    baseURL: '',
    timeout: 10000,
    headers: {
        Accept: "application/json, text/plain, */*",
        "Content-Type": "application/json",
        "X-Requested-With": "XMLHttpRequest"
    },
    // 数组格式参数序列化
    paramsSerializer: params => qs.stringify(params, { indices: false })
}
// 创建请求类
class Service {
    constructor() {
        this.httpInterceptorsRequest()
        this.httpInterceptors()
    }
    // 保存当前axios实例
    private static axiosInstance: AxiosInstance = axios.create(defultConfig)
    // 请求拦截方法
    private httpInterceptorsRequest(): void {
        Service.axiosInstance.interceptors.request.use((config: AxiosRequestConfig) => {
            // 显示加载动画
            NProgress.start()
            // token 相关代码
            const token = useUserStore().token
            if (!config.headers["Authorization"] && token) {
                config.headers["Authorization"] =
                    "Bearer " + token;
            }

            return config
        }, error => Promise.reject(error))
    }
    private httpInterceptors(): void {
        Service.axiosInstance.interceptors.response.use((response: AxiosResponse) => {
            NProgress.done()
            if (response.status === 200) {
                return Promise.resolve(response);
            } else {
                return Promise.reject(response);
            }

        }, (error) => {
            console.log(error);
            return Promise.reject(error)
        })
    }
    // 通用请求工具函数
    public request<T = any>(
        params: AxiosRequestConfig
    ): AxiosPromise<T> {
        // 单独处理自定义请求/响应回掉
        return Service.axiosInstance(params)
    }
    // get请求
    public get<T=any>(url:string,config?:AxiosRequestConfig):AxiosPromise<T>{
        return Service.axiosInstance.get(url,config)
    }
    // post请求
    public post<T=any>(url:string,config?:AxiosRequestConfig):AxiosPromise<T>{
        const params:AxiosRequestConfig={
            url,
            ...config,
            method:"post",
        }
        return Service.axiosInstance(params)
    }
    // put 请求
    public put<T=any>(url:string,config?:AxiosRequestConfig):AxiosPromise<T>{
        const params:AxiosRequestConfig={
            url,
            ...config,
            method:"put",
        }
        return Service.axiosInstance(params)
    }


}
export const http=new Service()